<?php namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesCommands;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Input;
use Response;
use Session;
use App\Models\UserRoles;

abstract class Controller extends BaseController {


	public function __construct(){
		header("Access-Control-Allow-Origin:*");
	}

	public function getUid()
	{
		// if(env("APP_DEBUG")){
		// 	return 1;
		// }
		return Session::has('user') ? Session::get('user')['id'] : 0 ;
	}

	public function getRoleName($id)
	{
		return UserRoles::where("id", $id)->get()[0]["role_name"];
	}

	public function moveImages($imgArr, $env, $user_id = null)
	{
		$uid = is_null($user_id) ? $this->getUid() : $user_id;
		$dir = base_path() . "/public/images/" . $uid ."/" . $env;
		is_dir($dir) || mkdir($dir, 0777, true);
		// chmod($dir, 0777);
		$path = base_path() . "/public/upload/";
		foreach ($imgArr as $key => $value) {
			$temp = $path . $value;
			$newPath = $dir . '/' . $value;
			if(is_file($temp)){
				rename($temp, $newPath);
			}
		}
	}

	use DispatchesCommands, ValidatesRequests;

	const NOT_LOGIN								= 0x000000;
	const USERNAME_PASSWORD_CAN_NOT_EMPTY		= 0x000001;
	const USERNAME_PASSWORD_IS_WRONG			= 0x000002;
	const ACCOUNT_IS_EXIST						= 0x000003;
	const NO_PRIVILAGE							= 0x000004;
	const NO_DATA 								= 0x000005;
	const DATA_IS_EXIST 						= 0x000006;
	const OLD_PASSWORD_IS_WRONG					= 0x000007;
	const PARAM_IS_WRONG						= 0x000008;
	const FILE_TYPE_IS_WRONG					= 0x000009;
	const VALIDATA_CODE_IS_WRONG				= 0x000010;
	const ERROR_TIMES_GT_THREE					= 0x000011;
	const ERROR_TIMES_GT_SIX					= 0x000012;
	const USERNAME_NOT_EXIST					= 0x000013;
	const PASSWORD_IS_WRONG						= 0x000014;
	const VALIDATA_CODE_IS_EMPTY				= 0x000015;
	const FREEZE_STATE							= 0x000016;
	const VALIDATA_CODE							= 0x000017;
	const DATA_IS_WRONG							= 0x000018;
	const USER_IS_FREEZE						= 0x000019;
	const USER_IS_LOCK							= 0x000020;
	const USER_IS_NOT_ACTIVATE					= 0x000021;
	const NUMBER_IS_BEYAND						= 0x000022;
	const IS_AUDITING							= 0x000023;
	const AUDIT_NOT_PASS						= 0x000024;
	const PASSWORD_NOT_SAME						= 0x000025;


	const SYSTEM_ERROR							= 0x100001;


	protected static $errorMsg = array(


		self::NOT_LOGIN							=> "未登陆",
		self::USERNAME_PASSWORD_CAN_NOT_EMPTY	=> "管理员帐号或密码不能为空!",
		self::USERNAME_PASSWORD_IS_WRONG		=> "帐号或密码错误!",
		self::SYSTEM_ERROR						=> "系统错误!",
		self::ACCOUNT_IS_EXIST					=> "帐号已存在!",
		self::NO_PRIVILAGE						=> "权限不够!",
		self::NO_DATA 							=> "数据不存在!",
		self::DATA_IS_EXIST 					=> "该数据已存在!",
		self::OLD_PASSWORD_IS_WRONG				=> "原始密码不正确！",
		self::PARAM_IS_WRONG					=> "参数错误!",
		self::FILE_TYPE_IS_WRONG				=> "文件类型错误!",
		self::VALIDATA_CODE_IS_WRONG			=> "验证码错误!",
		self::ERROR_TIMES_GT_THREE				=> "密码错误3次,错误6次账号将被冻结!",
		self::ERROR_TIMES_GT_SIX				=> "密码错误6次,账号将被冻结!",
		self::USERNAME_NOT_EXIST				=> "用户名不存在!",
		self::PASSWORD_IS_WRONG					=> "用户密码不正确!",
		self::VALIDATA_CODE_IS_EMPTY			=> "请输入验证码!",
		self::FREEZE_STATE						=> "账号已经被冻结，请稍后再试!",
		self::VALIDATA_CODE						=> "帐号或密码错误!",

		self::DATA_IS_WRONG						=> "请检查数据是否完整!",
		self::USER_IS_FREEZE					=> "账号已被冻结!",
		self::USER_IS_LOCK						=> "该账号被封号!",
		self::USER_IS_NOT_ACTIVATE				=> "账号未激活!",
		self::NUMBER_IS_BEYAND					=> "超过数量限制!",

		self::IS_AUDITING						=> "正在审核中，无法查看!",
		self::AUDIT_NOT_PASS					=> "审核未通过，无法查看!",
		self::PASSWORD_NOT_SAME					=> "两次密码不相同!"

	);

	public function json($data){
		$data["success"] = true;

		$type = Input::get("type");
		if($type == "jsonp"){
			return Response::json($data)->setCallback(Input::get("callback"));
		}
		return Response::json($data);
	}

	public static function error($code, $msg = '', $redirect = ''){
		$err = "";
		if(empty($msg)){
			$err = self::$errorMsg[$code];
		}else{
			$err = $msg;
		}
		$res = array('code' => $code, "msg" => $err, "success" => false);
		if($redirect != ''){
			$res["redirect"] = $redirect;
		}

		$type = Input::get("type");
		if($type == "jsonp"){
			return Response::json($res)->setCallback(Input::get("callback"));
		}
		return Response::json($res);
	}

}